﻿using System;
using HslCommunication.Core;
using HslCommunication.ModBus;
using HslCommunication.Reflection;

namespace HslCommunication.Profinet.Delta
{
	/// <summary>
	/// 台达PLC的串口通讯类，基于Modbus-Rtu协议开发，按照台达的地址进行实现。<br />
	/// The serial communication class of Delta PLC is developed based on the Modbus-Rtu protocol and implemented according to Delta's address.
	/// </summary>
	/// <remarks>
	/// 适用于DVP-ES/EX/EC/SS型号，DVP-SA/SC/SX/EH型号，地址参考API文档，同时地址可以携带站号信息，举例：[s=2;D100],[s=3;M100]，可以动态修改当前报文的站号信息。<br />
	/// Suitable for DVP-ES/EX/EC/SS models, DVP-SA/SC/SX/EH models, the address refers to the API document, and the address can carry station number information,
	/// for example: [s=2;D100],[s= 3;M100], you can dynamically modify the station number information of the current message.
	/// </remarks>
	/// <example>
	/// 地址的格式如下：
	/// <list type="table">
	///   <listheader>
	///     <term>地址名称</term>
	///     <term>地址代号</term>
	///     <term>示例</term>
	///     <term>地址进制</term>
	///     <term>字操作</term>
	///     <term>位操作</term>
	///     <term>备注</term>
	///   </listheader>
	///   <item>
	///     <term></term>
	///     <term>S</term>
	///     <term>S0-S1023</term>
	///     <term>10</term>
	///     <term>×</term>
	///     <term>√</term>
	///     <term></term>
	///   </item>
	///   <item>
	///     <term>输入继电器</term>
	///     <term>X</term>
	///     <term>X0-X377</term>
	///     <term>8</term>
	///     <term>×</term>
	///     <term>√</term>
	///     <term>只读</term>
	///   </item>
	///   <item>
	///     <term>输出继电器</term>
	///     <term>Y</term>
	///     <term>Y0-Y377</term>
	///     <term>8</term>
	///     <term>×</term>
	///     <term>√</term>
	///     <term></term>
	///   </item>
	///   <item>
	///     <term>定时器</term>
	///     <term>T</term>
	///     <term>T0-T255</term>
	///     <term>10</term>
	///     <term>√</term>
	///     <term>√</term>
	///     <term>如果是读位，就是通断继电器，如果是读字，就是当前值</term>
	///   </item>
	///   <item>
	///     <term>计数器</term>
	///     <term>C</term>
	///     <term>C0-C255</term>
	///     <term>10</term>
	///     <term>√</term>
	///     <term>√</term>
	///     <term>如果是读位，就是通断继电器，如果是读字，就是当前值</term>
	///   </item>
	///   <item>
	///     <term>内部继电器</term>
	///     <term>M</term>
	///     <term>M0-M4095</term>
	///     <term>10</term>
	///     <term>×</term>
	///     <term>√</term>
	///     <term></term>
	///   </item>
	///   <item>
	///     <term>数据寄存器</term>
	///     <term>D</term>
	///     <term>D0-D9999</term>
	///     <term>10</term>
	///     <term>√</term>
	///     <term>×</term>
	///     <term></term>
	///   </item>
	/// </list>
	/// 除此之外，地址可以携带站号信息，例如 s=2;D100，也是支持的。
	/// </example>
	// Token: 0x020000AF RID: 175
	public class DeltaDvpSerial : ModbusRtu
	{
		/// <summary>
		/// 实例化一个默认的对象<br />
		/// Instantiate a default object
		/// </summary>
		// Token: 0x06000CBD RID: 3261 RVA: 0x0004C058 File Offset: 0x0004A258
		public DeltaDvpSerial()
		{
			base.ByteTransform.DataFormat = DataFormat.CDAB;
		}

		/// <summary>
		/// 指定客户端自己的站号来初始化<br />
		/// Specify the client's own station number to initialize
		/// </summary>
		/// <param name="station">客户端自身的站号</param>
		// Token: 0x06000CBE RID: 3262 RVA: 0x0004C06F File Offset: 0x0004A26F
		public DeltaDvpSerial(byte station = 1) : base(station)
		{
			base.ByteTransform.DataFormat = DataFormat.CDAB;
		}

		/// <summary>
		/// 从寄存器里读取原始的字节数据内容，地址主要是 D,T,C，每个类型的地址范围不一样，具体参考API文档<br />
		/// Read the original byte data content from the register, the address is mainly D, T, C, 
		/// the address range of each type is different, please refer to the API document for details
		/// </summary>
		/// <param name="address">D,T,C 类型地址，举例：D100, C100</param>
		/// <param name="length">读取的地址长度，一个地址是2个字节</param>
		/// <returns>带有成功标志的字节数据</returns>
		// Token: 0x06000CBF RID: 3263 RVA: 0x0004C088 File Offset: 0x0004A288
		[HslMqttApi("ReadByteArray", "Read the original byte data content from the register, the address is mainly D, T, C")]
		public override OperateResult<byte[]> Read(string address, ushort length)
		{
			OperateResult<string> operateResult = DeltaHelper.PraseDeltaDvpAddress(address, 3);
			bool flag = !operateResult.IsSuccess;
			OperateResult<byte[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<byte[]>(operateResult);
			}
			else
			{
				result = base.Read(operateResult.Content, length);
			}
			return result;
		}

		/// <summary>
		/// 向寄存器里写入原始的字节数据内容，地址主要是 D,T,C，每个类型的地址范围不一样，具体参考API文档<br />
		/// Write the original byte data content to the register, the address is mainly D, T, C, 
		/// the address range of each type is different, please refer to the API document for details
		/// </summary>
		/// <param name="address">D,T,C 类型地址，举例：D100, C100</param>
		/// <param name="value">等待写入的原始字节值</param>
		/// <returns>返回写入结果</returns>
		// Token: 0x06000CC0 RID: 3264 RVA: 0x0004C0C8 File Offset: 0x0004A2C8
		[HslMqttApi("WriteByteArray", "Write the original byte data content to the register, the address is mainly D, T, C")]
		public override OperateResult Write(string address, byte[] value)
		{
			OperateResult<string> operateResult = DeltaHelper.PraseDeltaDvpAddress(address, 16);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<byte[]>(operateResult);
			}
			else
			{
				result = base.Write(operateResult.Content, value);
			}
			return result;
		}

		/// <summary>
		/// 从线圈里批量读取bool数据内容，地址主要是 X,Y,S,M,T,C，其中X,Y的地址是8进制的，X0-X377，Y0-Y377<br />
		/// Read the contents of bool data in batches from the coil, the address is mainly X, Y, S, M, T, C, where X, Y address is in octal, X0-X377, Y0-Y377
		/// </summary>
		/// <param name="address">X,Y,S,M,T,C，其中X,Y的地址是8进制的，X0-X377，Y0-Y377</param>
		/// <param name="length">数据长度</param>
		/// <returns>带有成功标识的bool[]数组</returns>
		// Token: 0x06000CC1 RID: 3265 RVA: 0x0004C108 File Offset: 0x0004A308
		[HslMqttApi("ReadBoolArray", "Read the contents of bool data in batches from the coil, the address is mainly X, Y, S, M, T, C")]
		public override OperateResult<bool[]> ReadBool(string address, ushort length)
		{
			OperateResult<string> operateResult = DeltaHelper.PraseDeltaDvpAddress(address, 1);
			bool flag = !operateResult.IsSuccess;
			OperateResult<bool[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<bool[]>(operateResult);
			}
			else
			{
				result = base.ReadBool(operateResult.Content, length);
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Delta.DeltaDvpSerial.Write(System.String,System.Boolean)" />
		// Token: 0x06000CC2 RID: 3266 RVA: 0x0004C148 File Offset: 0x0004A348
		[HslMqttApi("WriteBoolArray", "Read the contents of bool data in batches from the coil, the address is mainly X, Y, S, M, T, C")]
		public override OperateResult Write(string address, bool[] values)
		{
			OperateResult<string> operateResult = DeltaHelper.PraseDeltaDvpAddress(address, 15);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<bool[]>(operateResult);
			}
			else
			{
				result = base.Write(operateResult.Content, values);
			}
			return result;
		}

		/// <summary>
		/// 向线圈里写入bool数据内容，地址主要是 Y,S,M,T,C，其中Y的地址是8进制的，Y0-Y377，注意 X 地址不能写入！<br />
		/// Write bool data content to the coil, the address is mainly Y, S, M, T, C, where the address of Y is in octal, Y0-Y377, note that X address cannot be written!
		/// </summary>
		/// <param name="address">X,Y,S,M,T,C，其中X,Y的地址是8进制的，X0-X1037，Y0-Y1037</param>
		/// <param name="value">等待写入的Bool值</param>
		/// <returns>返回写入结果</returns>
		// Token: 0x06000CC3 RID: 3267 RVA: 0x0004C188 File Offset: 0x0004A388
		[HslMqttApi("WriteBool", "Write bool data content to the coil, the address is mainly Y, S, M, T, C")]
		public override OperateResult Write(string address, bool value)
		{
			OperateResult<string> operateResult = DeltaHelper.PraseDeltaDvpAddress(address, 5);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<bool[]>(operateResult);
			}
			else
			{
				result = base.Write(operateResult.Content, value);
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Core.IReadWriteNet.Write(System.String,System.Int16)" />
		// Token: 0x06000CC4 RID: 3268 RVA: 0x0004C1C8 File Offset: 0x0004A3C8
		[HslMqttApi("WriteInt16", "Write short data, returns whether success")]
		public override OperateResult Write(string address, short value)
		{
			OperateResult<string> operateResult = DeltaHelper.PraseDeltaDvpAddress(address, 6);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<bool[]>(operateResult);
			}
			else
			{
				result = base.Write(operateResult.Content, value);
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Core.IReadWriteNet.Write(System.String,System.UInt16)" />
		// Token: 0x06000CC5 RID: 3269 RVA: 0x0004C208 File Offset: 0x0004A408
		[HslMqttApi("WriteUInt16", "Write ushort data, return whether the write was successful")]
		public override OperateResult Write(string address, ushort value)
		{
			OperateResult<string> operateResult = DeltaHelper.PraseDeltaDvpAddress(address, 6);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<bool[]>(operateResult);
			}
			else
			{
				result = base.Write(operateResult.Content, value);
			}
			return result;
		}

		/// <inheritdoc />
		// Token: 0x06000CC6 RID: 3270 RVA: 0x0004C245 File Offset: 0x0004A445
		public override string ToString()
		{
			return string.Format("DeltaDvpSerial[{0}:{1}]", base.PortName, base.BaudRate);
		}
	}
}
